<template>
  <div class="favorites-page">
    <!-- 固定的顶部header -->
    <div class="header">
      <button class="back-btn" @click="goBack">
        ← 返回列表
      </button>
      <div class="header-title">
        <h1>收藏列表</h1>
        <p>这里显示收藏了当前钓点的用户</p>
      </div>
    </div>
    
    <!-- 可滚动的内容区域 -->
    <div class="scrollable-content">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <p>正在加载收藏用户...</p>
      </div>

      <!-- 错误状态 -->
      <div v-else-if="error" class="error-container">
        <div class="error-icon">⚠️</div>
        <p>{{ error }}</p>
        <button class="retry-btn" @click="loadCollectedSpots">
          重新加载
        </button>
      </div>

      <!-- 空状态 -->
      <div v-else-if="collectedUsers.length === 0" class="empty-container">
        <div class="empty-icon">👥</div>
        <h3>暂无收藏用户</h3>
        <p>还没有用户收藏此钓点</p>
      </div>

      <!-- 收藏用户列表 -->
      <div v-else class="users-list">
        <div 
          v-for="user in collectedUsers" 
          :key="user.userId"
          class="user-card"
        >
          <!-- 用户信息 -->
          <div class="user-info">
            <img :src="user.avatarUrl || '/placeholder-avatar.jpg'" 
                 :alt="user.nickname" 
                 class="user-avatar"
                 @error="$event.target.src = '/placeholder-avatar.jpg'"
            />
            <div class="user-details">
              <span class="user-name">{{ user.nickname || '匿名用户' }}</span>
              <span class="collect-date">收藏于 {{ formatDate(user.collectedAt) }}</span>
            </div>
          </div>

          <!-- 收藏状态 -->
          <div class="collect-status">
            <span :class="['status-badge', user.isCollected ? 'collected' : '']">
              {{ user.isCollected ? '已收藏' : '未收藏' }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { invoke } from '@tauri-apps/api/core'

interface CollectedUser {
  avatarUrl: string
  collectedAt: string
  isCollected: number
  nickname: string
  userId: string
}

interface ApiResponse {
  records: CollectedUser[]
  total: number
  size: number
  current: number
  pages: number
}

const router = useRouter()
const route = useRoute()
const collectedUsers = ref<CollectedUser[]>([])
const loading = ref(true)
const error = ref('')

// 从路由参数中获取 spotId
const spotId = ref<string>(route.query.spotId as string || '')

const loadCollectedSpots = async () => {
  loading.value = true
  error.value = ''

  try {
    // 检查是否有有效的 spotId
    if (!spotId.value) {
      throw new Error('无效的钓点ID')
    }

    const response = await invoke<ApiResponse>('get_collected_spots', {
      pageNum: 1,
      pageSize: 100,
      isCollected: 1,
      spotId: spotId.value // 添加 spotId 参数
    })
    
    collectedUsers.value = response.records || []
  } catch (err: any) {
    console.error('获取收藏用户失败:', err)
    error.value = err.message || '获取收藏用户失败，请稍后重试'
    
    // 显示全局错误提示
    try {
      await invoke('show_toast', {
        message: error.value,
        type: 'error'
      })
    } catch (toastErr) {
      console.error('显示错误提示失败:', toastErr)
    }
  } finally {
    loading.value = false
  }
}

const formatDate = (dateString: string) => {
  if (!dateString) return ''
  return new Date(dateString).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const goBack = () => {
  router.go(-1)
}

onMounted(() => {
  loadCollectedSpots()
})

</script>
<style scoped>
.favorites-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  color: #ffffff;
  background: #1e1e1e;
  overflow: hidden;
}

.header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 16px 24px;
  background: rgba(30, 30, 35, 0.95);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 100;
  flex-shrink: 0;
}

.back-btn {
  background: rgba(29, 185, 84, 0.2);
  color: #1db954;
  border: 1px solid #1db954;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
  white-space: nowrap;
}

.back-btn:hover {
  background: rgba(29, 185, 84, 0.3);
}

.header-title h1 {
  font-size: 20px;
  font-weight: 600;
  color: #ffffff;
  margin: 0 0 4px 0;
}

.header-title p {
  font-size: 14px;
  color: #b3b3b3;
  margin: 0;
}

.scrollable-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

/* 加载、错误、空状态样式 */
.loading-container,
.error-container,
.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #b3b3b3;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(29, 185, 84, 0.3);
  border-top: 3px solid #1db954;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-icon,
.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.retry-btn {
  background: rgba(29, 185, 84, 0.2);
  color: #1db954;
  border: 1px solid #1db954;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  margin-top: 16px;
}

.retry-btn:hover {
  background: rgba(29, 185, 84, 0.3);
}

/* 列表样式 */
.users-list {
  width: 100%;
  margin-bottom: 40px;
}

.user-card {
  background: rgba(40, 40, 45, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
  transition: all 0.3s ease;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.user-card:hover {
  background: rgba(45, 45, 50, 0.9);
  transform: translateY(-2px);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(29, 185, 84, 0.3);
}

.user-details {
  display: flex;
  flex-direction: column;
}

.user-name {
  font-weight: 600;
  color: #ffffff;
  font-size: 16px;
}

.collect-date {
  color: #b3b3b3;
  font-size: 14px;
}

.collect-status {
  display: flex;
  align-items: center;
}

.status-badge {
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.status-badge.collected {
  background: rgba(29, 185, 84, 0.2);
  color: #1db954;
  border: 1px solid rgba(29, 185, 84, 0.3);
}

@media (max-width: 768px) {
  .user-card {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .user-info {
    width: 100%;
  }
  
  .collect-status {
    align-self: flex-end;
  }
}
</style>
